package com.yike.product.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yike.common.pagination.PageInfo;
import com.yike.common.pagination.Paging;
import com.yike.product.convert.SpecificationConvert;
import com.yike.product.entity.Specification;
import com.yike.product.mapper.SpecificationMapper;
import com.yike.product.param.SpecificationPageParam;
import com.yike.product.service.SpecificationService;
import com.yike.common.base.BaseServiceImpl;
import com.yike.product.vo.SpecificationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自定义规格 服务实现类
 *
 * @author yike
 * @since 2024-07-31
 */
@Slf4j
@Service
public class SpecificationServiceImpl extends BaseServiceImpl<SpecificationMapper, Specification> implements SpecificationService {

    @Resource
    private SpecificationMapper specificationMapper;

    @Override
    public boolean save(String name) {
        if (StrUtil.isEmpty(name)) {
            return false;
        }
        Wrapper wrapper = Wrappers.lambdaQuery(Specification.class).eq(Specification::getName, name);
        Specification dbSpecification = specificationMapper.selectOne(wrapper);
        if (ObjectUtil.isNotNull(dbSpecification)) {
            return false;
        }
        Specification specification = new Specification();
        specification.setName(name);
        specificationMapper.insert(specification);
        return true;
    }

    @Override
    public Paging<SpecificationVo> getCustomPageList(SpecificationPageParam param) {
        //查询条件
        Wrapper wrapper = Wrappers.lambdaQuery(Specification.class).like(StrUtil.isNotBlank(param.getName()), Specification::getName, param.getName());
        //分页对象
        Page<Specification> page = new PageInfo<>(param);
        IPage<Specification> iPage = specificationMapper.selectPage(page, wrapper);
        List<SpecificationVo> goodsVos = SpecificationConvert.INSTANCE.convert(iPage.getRecords());
        return new Paging(iPage, goodsVos);
    }

}
